package com.qiyue.admin.biz.base;

import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.qiyue.admin.constant.AdminCommonConstant;
import com.qiyue.admin.constant.RedisConstant;
import com.qiyue.admin.entity.base.Element;
import com.qiyue.admin.entity.base.Menu;
import com.qiyue.admin.entity.base.ResourceAuthority;
import com.qiyue.admin.repository.base.ElementRepository;
import com.qiyue.common.biz.BaseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import javax.persistence.Query;
import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by y747718944 on 2018/2/7
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class ElementBiz  extends BaseBiz<ElementRepository,Element,Long>{

    @Autowired
    private  GroupTypeBiz groupTypeBiz;//角色类型分组
    @Autowired
    private MenuBiz menuBiz;

    @Cache(key=RedisConstant.PERMISSION_ELE+"u{1}")
    public List<Element> getAuthorityElementByUserId(String userId){
        Map<String,Object> params=new HashMap<>();
        Object[] authoritys=groupTypeBiz.getGroupIdByUserId(userId);
        if(authoritys.length>0) params.put("IN_resourceAuthority.authorityId",authoritys);
        params.put("EQ_resourceAuthority.resourceType","button");
        params.put("EQ_resourceAuthority.authorityType","group");
     List<Element> elementList=  this.findDistinctList(params)
                .stream()
                .map( element -> {
                    if (element.getMenu() != null){
                        element.setTitle(element.getMenu().getTitle());
                    }
                    return element;
                })
                .collect(Collectors.toList());
         return elementList;

    }


    @Cache(key=RedisConstant.PERMISSION_ELE+"u{1}{2}")
    public List<Element> getAuthorityElementByUserIdAndMenuId(String userId,String menuId){
        Map<String,Object> params=new HashMap<>();
//        params.put("IN_resourceAuthority.resourceId", groupTypeBiz.getGroupIdByUserId(userId));
        params.put("EQ_resourceAuthority.resourceType","button");
        params.put("EQ_resourceAuthority.authorityType","group");
        params.put("EQ_menuId",menuId);

        List<Element> elementList=  this.findDistinctList(params)
                .stream()
                .map( element -> {
                    element.setTitle(element.getMenu().getTitle());
                    return element;
                })
                .collect(Collectors.toList());
     return elementList;
    }

    @Override
    @Cache(key= RedisConstant.PERMISSION_ELE)
    public List<Element> findAll(){
        return  super.findAll();
    }


    /**
     *  重写save方法
     *  1、缓存问题
     *  2、级联问题
     * @param entity
     * @return
     */
    @Override
    @CacheClear(keys = {RedisConstant.PERMISSION_ELE,RedisConstant.PERMISSION})
    public Element save(Element entity){
        entity.setMenu(menuBiz.findOne(entity.getMenuId()));
        return super.save(entity);
    }






}
